//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#pragma once

#include "UI/Widgets/Widget.h"

namespace Engine
{
namespace UI
{

	class Picture;
	SHARED_POINTER( Picture );



	//
	// Picture
	//

	class Picture : public Widget
	{
		friend struct ::MetaData::PictureCfg;

	// variables
	protected:
		UIImage				_image;
		ColorStyle::EType	_state;


	// methods
	public:
		Picture (EWidget::type wgtType, const EngineSubSystemsRef ess);
		~Picture ();

		virtual bool ProcessMessage (const WidgetMsg &msg) override;

		
		static PicturePtr	New (const EngineSubSystemsRef ess);


	protected:
		bool _Arrange (const WidgetMsg &msg);
		bool _Update (const WidgetMsg &msg);
		bool _Draw (const WidgetMsg &msg);
		
		bool _SetColorStyle (const WidgetMsg &msg);
	};


	
/*
=================================================
	constructor
=================================================
*/
	inline Picture::Picture (EWidget::type wgtType, const EngineSubSystemsRef ess) :
		Widget(wgtType, ess), _state(ColorStyle::IDLE)
	{}
	
/*
=================================================
	constructor
=================================================
*/
	inline Picture::~Picture ()
	{}
	
/*
=================================================
	ProcessMessage
=================================================
*/
	inline bool Picture::ProcessMessage (const WidgetMsg &msg)
	{
		typedef WidgetMsg	WM;

		switch ( msg.GetType() )
		{
			case WM::UPDATE  :			return _Update( msg );
			case WM::DRAW :				return _Draw( msg );
			case WM::ARRANGE :			return _Arrange( msg );
			case WM::SET_COLOR_STYLE :	return _SetColorStyle( msg );
		}

		return Widget::ProcessMessage( msg );
	}

/*
=================================================
	New
=================================================
*/
	inline PicturePtr  Picture::New (const EngineSubSystemsRef ess)
	{
		return BaseObject::_New( new Picture( EWidget::PICTURE, ess ) );
	}
	
/*
=================================================
	_Arrange
=================================================
*/
	inline bool Picture::_Arrange (const WidgetMsg &msg)
	{
		typedef WidgetMsg		WM;

		ArrangeHelper const &	helper		= *msg.To< WM::Arrange >().helper;
		WidgetLayout			layout		= helper.GetLayout( _layouts );
		bool					auto_size	= helper.IsAutoSize( layout );
		
		_visibleForLayout = helper.IsVisibleForLayout( _layoutMask );

		if ( not _visibleForLayout )
			return true;

		_image.OnLoad();
		
		if ( auto_size )
		{
			if ( not _image.HasTexture() )
			{
				if ( not _ComputeInnerSize( layout, msg ) )
					RETURN_ERR( "not supported" );
			}
			else
			{
				layout.size = _image.FrameSize();
			}

			auto_size = false;
		}
		
		helper.Arrange( _region, layout, _parent->GetRegion(), _parent->GetDockRegion() );
		
		_dockRegion = _region;
		
		if ( _image.IsProportional() and _image.HasTexture() )
		{
			helper.ProportionalImageInRegion( _region, _image.FrameSize(), EAlign::CENTER, _dockRegion );
		}

		_stateChanged = true;

		_SendToAllAttached( msg );
		return true;
	}

/*
=================================================
	_Update
=================================================
*/
	inline bool Picture::_Update (const WidgetMsg &msg)
	{
		if ( not IsVisible() )
			return true;

		Graphics::BatchRender *	br = ESS()->GetGraphicsEngine()->GetBatchRender().ptr();
		
		_state = _enabled ? ColorStyle::IDLE : ColorStyle::DISABLED;
		
		_CheckChangedOnUpdate( msg );

		_SendToAllAttached( msg );
		return true;
	}
	
/*
=================================================
	_Draw
=================================================
*/
	inline bool Picture::_Draw (const WidgetMsg &msg)
	{
		if ( not IsVisible() )
			return true;

		Graphics::BatchRender *	br = ESS()->GetGraphicsEngine()->GetBatchRender().ptr();
		
		if ( _image.IsValid() )
			_image.Draw( br, _region, _state );
			
		_SendToAllAttached( msg );
		return true;
	}

/*
=================================================
	_SetColorStyle
=================================================
*/
	inline bool Picture::_SetColorStyle (const WidgetMsg &msg)
	{
		typedef WidgetMsg	WM;

		WM::SetColorStyle const &	scs = msg.To< WM::SetColorStyle >();

		switch ( scs.target )
		{
			case WM::SetColorStyle::IMAGE_CS :
				_image.SetColorStyle( scs.cs );
				return true;
		}
		return false;
	}


}	// UI
}	// Engine